/**
 * @author Nicolas Latorre (nicolas.latorre@usi.ch)
 * @author Nicolas Zimerman (zimerman@usi.ch)
 */
package ch.usi.inf.pf2.file.interaction;

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class ComponentRecognizer
{
	private final String text;
	private Token currentToken;
	private int matcherLength;
	
	public ComponentRecognizer(String text) {
		this.text = text;
	}
	
	public boolean find(int startFrom) {
		
		Pattern componentRegex = Pattern.compile("<[a-z]+>|[0-9]+");
		
		Matcher matcher = componentRegex.matcher(text.substring(startFrom));
		if(matcher.find()) {
			System.out.println("trovato --- " + matcher.group() +" --- length: " + matcher.group().length());
			matcherLength = matcher.group().length();
			if(matcher.group().equals("<and>")) {
				currentToken = new Token(TokenType.AND_GATE, "and");
				return true;
			}
			else if(matcher.group().equals("<or>")) {
				currentToken = new Token(TokenType.OR_GATE, "or");
				return true;
			}
			else if(matcher.group().equals("<not>")) {
				currentToken = new Token(TokenType.NOT_GATE, "not");
				return true;
			}
			else if(matcher.group().equals("<xor>")) {
				currentToken = new Token(TokenType.XOR_GATE, "xor");
				return true;
			}
			else if(matcher.group().equals("<nand>")) {
				currentToken = new Token(TokenType.NAND_GATE, "nand");
				return true;
			}
			else if(matcher.group().equals("<nor>")) {
				currentToken = new Token(TokenType.NOR_GATE, "nor");
				return true;
			}
			else if(matcher.group().equals("<input>")) {
				currentToken = new Token(TokenType.INPUT, "input");
				return true;
			}
			else if(matcher.group().equals("<output>")) {
				currentToken = new Token(TokenType.OUTPUT, "output");
				return true;
			}
			else if(matcher.group().equals("<wire>")) {
				currentToken = new Token(TokenType.WIRE, "wire");
				return true;
			}
			else if(matcher.group().matches("\\d+")) {
				currentToken = new Token(TokenType.CONSTANT, matcher.group());
				return true;
			}
		}
		return false;
	}

	public Token getToken() {
		return currentToken;
	}
	
	public int getMatcherLength() {
		return matcherLength;
	}
}